<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <meta content="webkit" name="renderer">
    <meta content="IE=edge,chrome=1" http-equiv="X-UA-Compatible">
    <meta content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0"
          name="viewport">
    <link rel="stylesheet" href="/asset/layui/css/layui.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header"></div>
                <div class="layui-card-body">

                    <!--顶部搜索框 start-->
                    <div class="layui-form">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户名</label>
                            <div class="layui-input-block">
                                <input type="text" placeholder="请输入" autocomplete="off" class="layui-input" name='username' data-search="true">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">真实姓名</label>
                            <div class="layui-input-block">
                                <input type="text" placeholder="请输入" autocomplete="off" class="layui-input" name='realName' data-search="true">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">手机号</label>
                            <div class="layui-input-block">
                                <input type="text" placeholder="请输入" autocomplete="off" class="layui-input" name='phone' data-search="true">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">性别</label>
                            <div class="layui-input-block">
                                <select name="sex" data-search="true">
                                    <option value=""></option>
                                    <option value="0">男</option>
                                    <option value="1">女</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <!--顶部搜索框 end-->

                    <!--顶部按钮-->
                    <div style="padding:10px;text-align: center">
                        <button class="layui-btn layui-btn-radius" data-type="search">
                            <i class="layui-icon layui-icon-search "></i>搜索
                        </button>
                        <button class="layui-btn layui-btn-radius layui-btn-warm" data-type="clear">
                            <i class="layui-icon layui-icon-close "></i>清空
                        </button>
                        <button class="layui-btn layui-btn-radius layui-btn-normal" data-type="save">
                            <i class="layui-icon layui-icon-addition "></i>新增
                        </button>
                        <button class="layui-btn layui-btn-radius layui-btn-danger" data-type="delAll">
                            <i class="layui-icon layui-icon-delete "></i>批量删除
                        </button>
                    </div>

                    <!--表格主体-->
                    <table id="table" lay-filter="table"></table>

                    <!--表格行基本操作模板-->
                    <script type="text/html" id="operation">
                        <a class="layui-btn layui-btn-xs" lay-event="get"><i class="layui-icon layui-icon-survey">查看</i></a>
                        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="update"><i class="layui-icon layui-icon-edit">更新</i></a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete">删除</i></a>
                    </script>

                    <!--性别模板-->
                    <script type="text/html" id="sexTPL">
                        <span>{{d.sex==0?'男':'女'}}</span>
                    </script>

                    <!--账号状态模板-->
                    <script type="text/html" id="statusTPL">
                        <input type="checkbox" lay-skin="switch" lay-text="ON|OFF" lay-filter="status" name="status" value="{{d.status}}" {{d.status== 0 ? 'checked' : '' }} >
                    </script>

                </div>
            </div>
        </div>
    </div>
</div>

<script src="/asset/layui/layui.js"></script>
<script src="/my/fast.js"></script>
<script>
    layui.use(() => {
        let $ = layui.$
        let table = layui.table;
        let form = layui.form;

        // 接口地址
        let page = UPMS_ADDRESS + '/sysUser/page';
        let del = UPMS_ADDRESS + '/sysUser/delete';
        let toSaveOrUpdate = './saveOrUpdate.html?id=';

        let setStatus = UPMS_ADDRESS + '/sysUser/setStatus';

        table.render({
            elem: 'table'
            , url: page
            , method: 'post'
            , contentType: 'application/json'
            , height: 'full-190'
            , cellMinWidth: 120
            , cols: [[
                {type: 'numbers'}
                , {type: 'checkbox'}
                , {field: 'id', title: 'ID', align: 'center', sort: true, hide: true}
                , {field: 'username', title: '用户名', align: 'center', sort: true}
                , {field: 'roleNames', title: '角色', align: 'center', sort: true}
                , {field: 'realName', title: '真实姓名', align: 'center', sort: true}
                , {field: 'phone', title: '手机号', align: 'center', sort: true}
                , {field: 'sex', title: '性别', align: 'center', sort: true, templet: '#sexTPL'}
                , {field: 'status', title: '账号状态', align: 'center', sort: true, templet: '#statusTPL'}
                , {field: 'createTime', title: '创建时间', align: 'center', sort: true}
                , {title: '操作', align: 'center', toolbar: '#operation', width: 250}
            ]]
            , page: true
            , toolbar: true
            , parseData: rs => { // 将原始数据解析成 table 组件所规定的数据
                return {
                    "code": rs.code,
                    "msg": rs.msg,
                    "count": rs.data.total,
                    "data": rs.data.records
                }
            }
            , response: {
                statusCode: 200 // 重新设定成功状态码，默认：0
            }
            , request: { // 重新设定分页参数
                pageName: 'current' // 页码参数名，默认：page
                , limitName: 'size' // 每页显示条数参数名，默认：limit
            }
            , even: true // 开启隔行背景
            , escape: true // 开启 xss 字符过滤
        });

        // layui--基础操作
        layuiBase($, table, 'table', 'table', $('[data-search=true]'), del, toSaveOrUpdate, obj => {
            console.log(obj);
        });

        // 账号状态
        form.on('switch(status)', function (obj) {
            let checked = obj.elem.checked;
            checked = checked ? 0 : 1;
            table.on('row(table)', function (obj) {
                let data = obj.data;
                postBody(setStatus, JSON.stringify({status: checked, id: data.id}));
            });
        });


    });
</script>
</body>
</html>
